package cn.edu.sctu.sctu_lysd.view.act;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import cn.edu.sctu.sctu_lysd.R;
import cn.edu.sctu.sctu_lysd.base.BaseActivity;
import cn.edu.sctu.sctu_lysd.biz.LoginBiz;
import cn.edu.sctu.sctu_lysd.databinding.ActivityLoginBinding;
import cn.edu.sctu.sctu_lysd.presenter.LoginPresenter;
import cn.edu.sctu.sctu_lysd.utils.JumpUtils;
import cn.edu.sctu.sctu_lysd.utils.ProgressUtil;

/**
 * Created by wangfuxin on 2017-04-06.
 */

public class LoginAct extends BaseActivity implements LoginBiz.View {
    private ActivityLoginBinding mBinding;
    private LoginBiz.Presenter mPresenter;
    private ProgressUtil progressUtil;
    private ProgressDialog progressDialog;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_login;
    }

    @Override
    protected void initAfter(Object bind) {
        mBinding = (ActivityLoginBinding) bind;

        mBinding.idLoginSubmitButton.setOnClickListener(this);
        mBinding.idCancel.setOnClickListener(this);
        mBinding.resgister.setOnClickListener(this);
//        mBinding.forgetPassword.setOnClickListener(this);

        progressUtil = ProgressUtil.getInstance(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        initPresenter(new LoginPresenter(this, this));
        mPresenter.start();
    }

    @Override
    public void initPresenter(LoginBiz.Presenter presenter) {

        this.mPresenter = presenter;

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.cancel();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.id_cancel:

               finish();

                break;
            case R.id.id_login_submit_button:

                progressDialog = progressUtil.getProgressDialog(ProgressDialog.STYLE_HORIZONTAL, true, "ssss");

                progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        /**
                         * 如果 请求 中途取消  终止请求
                         */
                        mPresenter.cancel();
                    }
                });

                mPresenter.login(mPresenter.getInputSting(mBinding.idLoginUsernameEdit),
                        mPresenter.getInputSting(mBinding.idLoginPasswordEdit));

                break;

//            case R.id.forget_password:
//
////                Intent intent = new Intent(LoginAct.this,F.class);
////
////                startActivity(intent);
//
//                break;

            case R.id.resgister:

                JumpUtils.jumpActivity(LoginAct.this, RegisterAct.class);
                break;

            default:

                break;
        }

    }


    @Override
    public void loginSuccess() {

        progressUtil.dismiss(progressDialog);

        Toast.makeText(this, "login success", Toast.LENGTH_SHORT).show();

        JumpUtils.jumpActivity(this,IndexActivity.class);

        finish();


    }

    @Override
    public void loginFailed(int code) {

        progressUtil.dismiss(progressDialog);

        if (code == LoginPresenter.LOGIN_FAILED_USER_NOT_FOUND) {

            Toast.makeText(this, "账号或者 错误", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "网络 错误", Toast.LENGTH_SHORT).show();

        }


    }

    @Override
    public void connectRongyun(String chatToken) {
        connect(chatToken);
    }
}
